/*
 * =====================================================================================
 *
 *       Filename:  regex.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  09/28/15 15:52:07
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Andy (gk), andy_y_li@163.com
 *        Company:  RD
 *
 * =====================================================================================
 */
#include <stdio.h>
#include <sys/types.h>
#include <regex.h>

int main()
{
    int status, i;
    int cflags = REG_EXTENDED;
    regmatch_t pmatch[1];
    const size_t nmatch = 1;
    regex_t reg;
    //const char * pattern = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*.\\w+([-.]\\w+)*$";
    const char *pattern = "(\\w+)@.{5}.(.*)";
    const char *buf = "devid122sfdf333@gmail.com";

    regcomp(&reg, pattern, cflags);
    status = regexec(&reg, buf, nmatch, pmatch, 0);
    if (status == REG_NOMATCH) {
        printf("NO Match!");
    }
    else if (status == 0) {
        printf("Match:\n");
        for (i=pmatch[0].rm_so; i < pmatch[0].rm_eo; i++){
            putchar(buf[i]);
        }
        printf("\n");
    }
    regfree(&reg);
    return 0;
}


